package com.xiangpq.pattern.structuralPatterns.proxyPattern.dynamicProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @author XPQ
 * @function
 * @since 2024-03-31
 */
public class ProcessSell implements InvocationHandler {

    // 目标对象
    private Object target = null;

    public ProcessSell() {
    }

    public ProcessSell(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object ret = null;
        Object[] objects = Arrays.copyOf(args, args.length + 1);
        objects[objects.length - 1] = 5000;
        // 执行目标方法
        ret = method.invoke(target, objects[objects.length - 1]);
        float price = (float) ret;
        // 利润是 25
        ret = (price + 25) * (int) objects[0];
        System.out.println("用户购买的价格是：" + ret);
        return ret;
    }
}
